package mil.af.amc.adpetracker.service;

import mil.af.amc.adpetracker.dao.ManufacturerDao;
import mil.af.amc.adpetracker.dao.ModelDao;
import mil.af.amc.adpetracker.exceptions.FileException;
import mil.af.amc.adpetracker.exceptions.ModelNotFoundException;
import mil.af.amc.adpetracker.model.Manufacturer;
import mil.af.amc.adpetracker.model.Model;
import org.apache.struts.upload.FormFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Service("modelService")
public class ModelServiceImpl implements ModelService {

    private ModelDao modelDao;

    @Autowired
    public void setModelDao(ModelDao modelDao) {
        this.modelDao = modelDao;
    }

    private ManufacturerDao manufacturerDao;

    @Autowired
    public void setManufacturerDao(ManufacturerDao manufacturerDao) {
        this.manufacturerDao = manufacturerDao;
    }

    public Model getById(int id) {

        return modelDao.getById(id);
    }

    public List<Model> getAll() {

        return modelDao.getAll();

    }

    public List<Model> getAllByModel(int modelId) {

        Model model = modelDao.getById(modelId);
        return getAllByModel(model);
    }

    public List<Model> getAllByModel(Model model) {

        return modelDao.getAllByModel(model);

    }

    public List<Model> getAllByManufacturer(int manufacturerId) {
        Manufacturer manu = manufacturerDao.getById(manufacturerId);
        return modelDao.getAllByManufacturer(manu);
    }

    public List<Model> getByManufacturer(Manufacturer manu) {

        return modelDao.getAllByManufacturer(manu);

    }

    public void createModel(Map modelMap) throws FileException {

        Model model = new Model();
        model.setName((String) modelMap.get("modelName"));

        int manufacturerId = (Integer) modelMap.get("manufacturerId");
        Manufacturer manufacturer = manufacturerDao.getById(manufacturerId);
        model.setManufacturer(manufacturer);

        model.setProcessorSpeed((Integer) modelMap.get("processorSpeed"));

/*        FormFile file = (FormFile) modelMap.get("image");
        try {
            model.setImage(file.getFileData());
        } catch (IOException e) {
            throw new FileException();
        }*/

        modelDao.saveOrUpdate(model);

    }

    public void editModel(Map modelMap) throws FileException, ModelNotFoundException {

        int modelId = (Integer) modelMap.get("modelId");
        Model model = modelDao.getById(modelId);

        if (model==null) {
            throw new ModelNotFoundException();
        }

        model.setName((String) modelMap.get("modelName"));

        int manufacturerId = (Integer) modelMap.get("manufacturerId");
        Manufacturer manufacturer = manufacturerDao.getById(manufacturerId);
        model.setManufacturer(manufacturer);

        model.setProcessorSpeed((Integer) modelMap.get("processorSpeed"));

        FormFile file = (FormFile) modelMap.get("image");
        try {
            model.setImage(file.getFileData());
        } catch (IOException e) {
            throw new FileException();
        }

        modelDao.saveOrUpdate(model);

    }
}
